package com.blockchain.console.servlets.system.sup;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.console.service.system.SupUserManage;
import com.blockchain.console.service.trading.TradingManage;
import com.blockchain.console.service.trading.entity.XlbEntity;
import com.blockchain.console.servlets.system.AbstractSystemServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.parser.IntegerParser;

@Right(id = "C_SUP_ADDSUPUSER", name = "控盘账户管理-添加")
public class AddSupUser extends AbstractSystemServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		TradingManage tsradingManage = serviceSession.getService(TradingManage.class);
		PagingResult<XlbEntity> xlbs = tsradingManage.xlbSearch();
		request.setAttribute("coinList", xlbs);
		forwardView(request, response, getClass());
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		SupUserManage supUserManage = serviceSession
				.getService(SupUserManage.class);
		TradingManage tsradingManage = serviceSession.getService(TradingManage.class);
		String accountName = request.getParameter("accountName");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String status = request.getParameter("status");
		String sjh = request.getParameter("sjh");
		int coinId = IntegerParser.parse(request.getParameter("coinId"));
		String coinName = tsradingManage.getXlb(coinId).zwm;
		int supUserId = 0;
		try {
			supUserId = supUserManage.add(accountName, password, name, status,sjh, coinId, coinName);
		} catch (Throwable e) {
			if (e instanceof ParameterException
					|| e instanceof LogicalException) {
				getController().prompt(request, response, PromptLevel.WARRING,
						e.getMessage());
				processGet(request, response, serviceSession);
			} else {
				super.onThrowable(request, response, e);
			}
		}
		if (supUserId > 0) {
			sendRedirect(request, response,
					getController().getURI(request, SupUserList.class));
		}
	}
}
